package com.feshfans.controller.special;

import com.feshfans.controller.Controller;
import com.feshfans.xmpp.client.ClientFactory;
import com.feshfans.xmpp.client.MessageClient;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import org.jivesoftware.smack.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 发送临时消息窗口
 */
public class TempMessageController extends Controller {

    private static final Logger Log = LoggerFactory.getLogger(TempMessageController.class);

    @FXML private TextField jidTextField;
    @FXML private TextArea messageTextArea;

    private MessageClient messageClient;

    public TempMessageController(){
        // 创建 messageClient 对象
        messageClient = ClientFactory.getClient(ClientFactory.ClientType.Message);
    }

    @FXML
    private void sendTempMessage(ActionEvent event){

        String jid = jidTextField.getText();
        String message = messageTextArea.getText();

        // 如果没有填写发送人或者消息内容为空，则直接返回
        if(StringUtils.isEmpty(jid)|| StringUtils.isEmpty(message)){
            return;
        }
        try {
            messageClient.sendMessage(jid,message);
            return;
        } catch (Exception e) {
            Log.error("发送消息失败,jid:{},message:{}",jid,message,e);
        }
        // 发送失败提醒
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setContentText("发送消息失败");
        alert.show();
    }

}
